//
//  YBNavMenuView.m
//  HongLiQuan
//
//  Created by 游兵 on 2017/6/17.
//  Copyright © 2017年 YBing. All rights reserved.
//

#import "YBNavMenuView.h"
#import "Define.h"
#import <ReactiveObjC.h>

@interface YBNavMenuView ()

@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) UIButton *selectBtn;


@end

@implementation YBNavMenuView

- (void)setSelectBtn:(UIButton *)selectBtn
{
    _selectBtn.selected = NO;
    _selectBtn = selectBtn;
    selectBtn.selected = YES;
}

- (UIView *)bottomLine
{
    if (_bottomLine==nil) {
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-2, 80, 2)];
        _bottomLine.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bottomLine];
    }
    return _bottomLine;
}

- (void)setItems:(NSArray<NSString *> *)items
{
    _items = items;
    __block CGFloat max_X = 0;
    
    
    [items enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.numberOfLines = 0;
        CGFloat w = [obj sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}].width+24;
        btn.frame = CGRectMake(max_X, 0, w, self.height);
        max_X += w;
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitle:obj forState:UIControlStateNormal];
        btn.tag = 10+idx;
        YBWeakSelf
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            
            if (weakSelf.selectBtn == x) return;
            weakSelf.selectIndex = idx;
            
            if (weakSelf.didClicked) {
                weakSelf.didClicked(idx);
            }
        }];
        [self addSubview:btn];
    }];
    self.selectIndex = 0;
    
}

- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    self.selectBtn = [self viewWithTag:10+selectIndex];
    self.bottomLine.x = self.selectBtn.x;
    self.bottomLine.width = self.selectBtn.width;
    if (self.bottomLine.center.x>self.width/2.0) {
        if (self.bottomLine.center.x<self.contentSize.width-self.width/2) {
            [self setContentOffset:CGPointMake(self.bottomLine.center.x-self.width/2, 0) animated:YES];
        } else {
            [self setContentOffset:CGPointMake(self.contentSize.width-self.width, 0) animated:YES];
        }
    } else {
        [self setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
    
}

- (instancetype)initWithFrame:(CGRect)frame items:(NSArray<NSString *> *)items didClicked:(void (^)(NSInteger))didClicked
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kNavColor;
        self.didClicked = didClicked;
        self.items = items;
        self.contentSize = CGSizeMake(CGRectGetMaxX([self viewWithTag:10+items.count-1].frame), 0);
        self.showsHorizontalScrollIndicator = NO;
        self.bounces = NO;
    }
    return self;
}

@end
